package cn.sypt.yuhao.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import cn.itcast.commons.CommonUtils;
import cn.itcast.mail.Mail;
import cn.itcast.mail.MailUtils;
import cn.sypt.yuhao.domain.User;
import cn.sypt.yuhao.model.*;

public class grglCl extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		String flag=request.getParameter("flag");
		UserBeanBO usb=new UserBeanBO();
		User ub=new User();
		GrglBO gb=new GrglBO();
		String msg="";
		
		if(flag.equals("update"))
		{
			ub=(User) request.getSession().getAttribute("user");
			String id=(String) ub.getUid();
			String uname=request.getParameter("uname");
			String uemail=request.getParameter("uemail");
			String uaddress=request.getParameter("uaddress");
			String ubirth=request.getParameter("ubirth");
			String uphone=request.getParameter("uphone");
			if(gb.updateUser(id, uname, uemail, uaddress, ubirth, uphone))
			{
				msg="修改成功!";
				request.setAttribute("msg",msg);
				ub=usb.getUser(uname);
				request.getSession().setAttribute("user", ub);
				request.getRequestDispatcher("/home/grxxxg.jsp").forward(request, response);
			}
			
			
		}
		else if(flag.equals("yxyzm"))
		{
			String yxyzm=new Yxyzm().getYxyzm();
			ub=(User) request.getSession().getAttribute("user");
			String to =ub.getUemail() ;//发送对象，需要用户的邮箱....自己根据需要填写
			String subject ="来自高校实验学习平台的重置密码所需的验证码邮件";//邮件的主题....自己根据需要填写
			String content ="您好 "+ub.getUname()+"您的重置密码的邮箱验证码为"+yxyzm;//邮件的内容....自己根据需要填写
			JavaMail jm=new JavaMail();
			jm.send(to, subject, content);
			request.getSession().setAttribute("yxyzm", yxyzm);
			request.getRequestDispatcher("/home/mmcz.jsp").forward(request, response);
			
		}
		else if(flag.equals("czmm"))
		{
			ub=(User) request.getSession().getAttribute("user");
			//获取新密码
			String upass=request.getParameter("upass");
			//获取原密码
			String ypass=request.getParameter("ypass");
			//获取邮箱验证码
			String yxyzm=request.getParameter("yxyzm");
			//判断原密码是否与用户原密码一致
			if(ypass.equals(ub.getUpass()))
			{
			    
				String syxyzm=(String) request.getSession().getAttribute("yxyzm");	
				if(syxyzm!=null)
				{
				if(syxyzm.equals(yxyzm))
				{
					gb.czmm(upass, ub.getUid());
				msg="密码重置成功!";
				request.setAttribute("msg",msg);
				//将重置的密码放入sesion中的ub中
				ub.setUpass(upass);
				System.out.println(yxyzm+","+syxyzm+","+upass+","+ypass);
				request.setAttribute("user", ub);
				request.getRequestDispatcher("/home/mmcz.jsp").forward(request, response);
				}
				}
				else
				{
					msg="验证码不对!";
					request.setAttribute("msg", msg);
					request.getRequestDispatcher("/home/mmcz.jsp").forward(request, response);
				}
			}
			else
			{
				msg="原密码与用户的原密码不一致!";
				request.setAttribute("msg",msg);
				request.getRequestDispatcher("/home/mmcz.jsp").forward(request, response);
			}
			
			
		}else if(flag.equals("gxsb"))
		{
			
			ub=(User) request.getSession().getAttribute("user");
			//�ж��Ƿ����������ʵĶ���
			String flag1=request.getParameter("flag1");
			String sbid1=request.getParameter("sbid");
			ArrayList al=new ArrayList();
			if(flag1!=null)
			{
				if(flag1.equals("dl"))
				{		
					gb.dlSuibi(sbid1);
					al=gb.getSuibi(ub.getUid());
					
					
					response.getWriter().print("<script language=javascript>self.location=document.referrer;</script>");					
					return;
				}else
				{
				String sbtext=request.getParameter("sbtext");
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//�������ڸ�ʽ
				String date=df.format(new Date());// new Date()Ϊ��ȡ��ǰϵͳʱ��				
				String sbid=new Uuid().uuid();			
				gb.addSuibi(sbid,ub.getUid(),date,sbtext);
				al=new ArrayList();
				
				
				al=gb.getSuibi(ub.getUid());
				response.getWriter().print("<script language=javascript>self.location=document.referrer;</script>");
				return;
				}								
			}
			al=gb.getSuibi(ub.getUid());
			request.setAttribute("suibi", al);
			request.getRequestDispatcher("/home/gxsb.jsp").forward(request, response);
			
			
		}else if(flag.equals("xztx"))
		{
			
			String uimage=request.getParameter("uimage");
			ub=(User) request.getSession().getAttribute("user");
			String flag1=request.getParameter("flag1");
			if(uimage!=null)
			{
		       gb.updateTx(ub.getUid(), uimage);
		       ub.setUimage(uimage);
			}
			if(flag1!=null)
			{
			if(flag1.equals("sctxtp"))
			{
				// 创建工具
				FileItemFactory factory = new DiskFileItemFactory();
				/*
				 * 2. 创建解析器对象
				 */
				ServletFileUpload sfu = new ServletFileUpload(factory);
				/*
				 * 3. 解析request得到List<FileItem>
				 */
				List<FileItem> fileItemList = null;
				
				
				try {
					fileItemList = sfu.parseRequest(request);
				} catch (FileUploadException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				// 获取文件名
				FileItem sptpfile = fileItemList.get(0);// 获取图片
				String txtpurl = sptpfile.getName();
				// 截取文件名，因为部分浏览器上传的绝对路径
				int index = txtpurl.lastIndexOf("\\");
				if (index != -1) {
					txtpurl = txtpurl.substring(index + 1);
				}
				// 给文件名添加uuid前缀，避免文件同名现象
				txtpurl = CommonUtils.uuid() + "_" + txtpurl;
				String savepath = this.getServletContext().getRealPath("/home/img/touxiang");
				/*
				 * 2. 创建目标文件
				 */
				File destFile = new File(savepath, txtpurl);
				/*
				 * 3. 保存文件
				 */
				try {
					sptpfile.write(destFile);// 它会把临时文件重定向到指定的路径，再删除临时文件
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
				
				gb.updateTx(ub.getUid(), txtpurl);
				ub.setUimage(txtpurl);
			}
			}
	
			
			request.getRequestDispatcher("/home/xztx.jsp").forward(request, response);
		}		
		else if(flag.equals("dzsp"))
		{
			String flag1=(String)request.getParameter("flag1");
			String spid=(String)request.getParameter("spid");
			if(flag1!=null)
			{
			if(flag1.equals("dldz"))
			{
				boolean a=gb.dlDzXzsp(flag1, spid);
				System.out.println("flag1="+flag1+",spid="+spid+",a="+a);
			}
			}
			//��õ�����Ƶid����
			ArrayList al=new ArrayList();
			//��õ�����Ƶ��Video�ļ���
			ArrayList bl=new ArrayList();
			//���session�е��û�UserBean
			ub=(User) request.getSession().getAttribute("user");
			//����pageSize
			int pageSize=4;
			//��ȡҳ��
			String s_pageNow=request.getParameter("pageNow");
			//����ҳ��
			int pageNow=1;
			if(s_pageNow!=null)
			{
				
			pageNow=Integer.parseInt(s_pageNow);
			
			al=gb.getDzspid(ub.getUid(),pageSize,pageNow);
			}else
			{
				al=gb.getDzspid(ub.getUid(),pageSize,pageNow);
			}
			request.setAttribute("pageNow",pageNow+"");
			bl=gb.getVideo(al);
			int pageCount=gb.getdzpageCount(pageSize, ub.getUid());
			
			request.setAttribute("pageCount", pageCount+"");
			request.setAttribute("dzsp", bl);
			request.getRequestDispatcher("/home/dzsp.jsp").forward(request, response);
		}else if(flag.equals("xzsp"))
		{
			
			String flag1=(String)request.getParameter("flag1");
			String spid=(String)request.getParameter("spid");
			System.out.println("flag1="+flag1+",spid="+spid);
			if(flag1!=null)
			{
			if(flag1.equals("dlxz"))
			{
				boolean a=gb.dlDzXzsp(flag1, spid);
				System.out.println("flag1="+flag1+",spid="+spid+",a="+a);
			}
			}
			//���������Ƶid����
			ArrayList al=new ArrayList();
			//���������Ƶ��Video�ļ���
			ArrayList bl=new ArrayList();
			//���session�е��û�UserBean
			ub=(User) request.getSession().getAttribute("user");
			//����pageSize
			int pageSize=4;
			int pageNow=1;
			//��ȡҳ��
			String s_pageNow=request.getParameter("pageNow");
			
			if(s_pageNow!=null)
			{
			pageNow=Integer.parseInt(s_pageNow);
			
			
			al=gb.getxzspid(ub.getUid(),pageSize,pageNow);
			}else
			{
				al=gb.getxzspid(ub.getUid(),pageSize,pageNow);
			}
			request.setAttribute("pageNow",pageNow+"");
			bl=gb.getVideo(al);
			int pageCount=gb.getxzpageCount(pageSize, ub.getUid());
			request.setAttribute("pageCount", pageCount+"");
			request.setAttribute("xzsp", bl);
			request.getRequestDispatcher("/home/xzsp.jsp").forward(request, response);
		}else if(flag.equals("scspgl"))
		{
			
			ArrayList al=new ArrayList();
			ub=(User)request.getSession().getAttribute("user");
			String uname=ub.getUname();
			System.out.println(uname);
			int pageSize=4;
			int pageNow=1;
			//获取当前页码
			String s_pageNow=request.getParameter("pageNow");
			
			if(s_pageNow!=null)
			{
			pageNow=Integer.parseInt(s_pageNow);
			
			
			al=gb.scspJh(uname,pageNow,pageSize);
			}else
			{
				al=gb.scspJh(uname,pageNow,pageSize);
			}
			request.setAttribute("pageNow",pageNow+"");
			
			int pageCount=gb.getscsppageCount(pageSize, uname);
			request.setAttribute("pageCount", pageCount+"");
			request.setAttribute("scspgl", al);
			request.getRequestDispatcher("/home/scspgl.jsp").forward(request, response);
			
		
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}



}
